home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 104 / MacAddict_104_2005-04.iso / Software / Internet & Communication / WordPress 1.2.2 freeware.dmg / wordpress / wp-includes / template-functions-post.php < prev    next >
Encoding:
PHP Script  |  2004-05-10  |  17.0 KB  |  499 lines

  1. <?php
  2.  
  3. // Default filters
  4. add_filter('the_title', 'convert_chars');
  5. add_filter('the_title', 'trim');
  6.  
  7. add_filter('the_title_rss', 'strip_tags');
  8.  
  9. add_filter('the_content', 'convert_smilies');
  10. add_filter('the_content', 'convert_chars');
  11. add_filter('the_content', 'wpautop');
  12.  
  13. add_filter('the_excerpt', 'convert_smilies');
  14. add_filter('the_excerpt', 'convert_chars');
  15. add_filter('the_excerpt', 'wpautop');
  16.  
  17. function get_the_password_form() {
  18.     $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
  19.     <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
  20.     <p><label>' . __("Password:") . ' <input name="post_password" type="text" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
  21.     </form>
  22.     ';
  23.     return $output;
  24. }
  25.  
  26. function the_ID() {
  27.     global $id;
  28.     echo $id;
  29. }
  30.  
  31. function the_title($before = '', $after = '', $echo = true) {
  32.     $title = get_the_title();
  33.     if (!empty($title)) {
  34.         $title = apply_filters('the_title', $before . $title . $after);
  35.         if ($echo)
  36.             echo $title;
  37.         else
  38.             return $title;
  39.     }
  40. }
  41.  
  42. function the_title_rss() {
  43.     $title = get_the_title();
  44.     $title = apply_filters('the_title', $title);
  45.     $title = apply_filters('the_title_rss', $title);
  46.     echo $title;
  47. }
  48.  
  49. function get_the_title() {
  50.     global $post;
  51.     $output = stripslashes($post->post_title);
  52.     if (!empty($post->post_password)) { // if there's a password
  53.         $output = 'Protected: ' . $output;
  54.     }
  55.     return $output;
  56. }
  57.  
  58. function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  59.     $content = get_the_content($more_link_text, $stripteaser, $more_file);
  60.     $content = apply_filters('the_content', $content);
  61.     $content = str_replace(']]>', ']]>', $content);
  62.     echo $content;
  63. }
  64.  
  65. function the_content_rss($more_link_text='(more...)', $stripteaser=0, $more_file='', $cut = 0, $encode_html = 0) {
  66.     $content = get_the_content($more_link_text, $stripteaser, $more_file);
  67.     $content = apply_filters('the_content', $content);
  68.     if ($cut && !$encode_html) {
  69.         $encode_html = 2;
  70.     }
  71.     if ($encode_html == 1) {
  72.         $content = htmlspecialchars($content);
  73.         $cut = 0;
  74.     } elseif ($encode_html == 0) {
  75.         $content = make_url_footnote($content);
  76.     } elseif ($encode_html == 2) {
  77.         $content = strip_tags($content);
  78.     }
  79.     if ($cut) {
  80.         $blah = explode(' ', $content);
  81.         if (count($blah) > $cut) {
  82.             $k = $cut;
  83.             $use_dotdotdot = 1;
  84.         } else {
  85.             $k = count($blah);
  86.             $use_dotdotdot = 0;
  87.         }
  88.         for ($i=0; $i<$k; $i++) {
  89.             $excerpt .= $blah[$i].' ';
  90.         }
  91.         $excerpt .= ($use_dotdotdot) ? '...' : '';
  92.         $content = $excerpt;
  93.     }
  94.     $content = str_replace(']]>', ']]>', $content);
  95.     echo $content;
  96. }
  97.  
  98. function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  99.     global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
  100.     global $preview, $cookiehash;
  101.     global $pagenow;
  102.     $output = '';
  103.  
  104.     if (!empty($post->post_password)) { // if there's a password
  105.         if ($_COOKIE['wp-postpass_'.$cookiehash] != $post->post_password) {  // and it doesn't match the cookie
  106.             $output = get_the_password_form();
  107.             return $output;
  108.         }
  109.     }
  110.  
  111.     if ($more_file != '') {
  112.         $file = $more_file;
  113.     } else {
  114.         $file = $pagenow; //$_SERVER['PHP_SELF'];
  115.     }
  116.     $content = $pages[$page-1];
  117.     $content = explode('<!--more-->', $content);
  118.     if ((preg_match('/<!--noteaser-->/', $post->post_content) && ((!$multipage) || ($page==1))))
  119.         $stripteaser = 1;
  120.     $teaser = $content[0];
  121.     if (($more) && ($stripteaser))
  122.         $teaser = '';
  123.     $output .= $teaser;
  124.     if (count($content)>1) {
  125.         if ($more) {
  126.             $output .= '<a id="more-'.$id.'"></a>'.$content[1];
  127.         } else {
  128.             $output .= ' <a href="'. get_permalink() . "#more-$id\">$more_link_text</a>";
  129.         }
  130.     }
  131.     if ($preview) { // preview fix for javascript bug with foreign languages
  132.         $output =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $output);
  133.     }
  134.     return $output;
  135. }
  136.  
  137. function the_excerpt() {
  138.     echo apply_filters('the_excerpt', get_the_excerpt());
  139. }
  140.  
  141. function the_excerpt_rss($cut = 0, $encode_html = 0) {
  142.     $output = get_the_excerpt(true);
  143.  
  144.     $output = convert_chars($output);
  145.     if ($cut && !$encode_html) {
  146.         $encode_html = 2;
  147.     }
  148.     if ($encode_html == 1) {
  149.         $output = htmlspecialchars($output);
  150.         $cut = 0;
  151.     } elseif ($encode_html == 0) {
  152.         $output = make_url_footnote($output);
  153.     } elseif ($encode_html == 2) {
  154.         $output = strip_tags($output);
  155.         $output = str_replace('&', '&', $output);
  156.     }
  157.     if ($cut) {
  158.         $excerpt = '';
  159.         $blah = explode(' ', $output);
  160.         if (count($blah) > $cut) {
  161.             $k = $cut;
  162.             $use_dotdotdot = 1;
  163.         } else {
  164.             $k = count($blah);
  165.             $use_dotdotdot = 0;
  166.         }
  167.         for ($i=0; $i<$k; $i++) {
  168.             $excerpt .= $blah[$i].' ';
  169.         }
  170.         $excerpt .= ($use_dotdotdot) ? '...' : '';
  171.         $output = $excerpt;
  172.     }
  173.     $output = str_replace(']]>', ']]>', $output);
  174.     echo apply_filters('the_excerpt_rss', $output);
  175. }
  176.  
  177. function get_the_excerpt($fakeit = true) {
  178.     global $id, $post;
  179.     global $cookiehash;
  180.     $output = '';
  181.     $output = stripslashes($post->post_excerpt);
  182.     if (!empty($post->post_password)) { // if there's a password
  183.         if ($_COOKIE['wp-postpass_'.$cookiehash] != $post->post_password) {  // and it doesn't match the cookie
  184.             $output = __('There is no excerpt because this is a protected post.');
  185.             return $output;
  186.         }
  187.     }
  188.  
  189.     // If we haven't got an excerpt, make one in the style of the rss ones
  190.     if (($output == '') && $fakeit) {
  191.         $output = $post->post_content;
  192.         $output = strip_tags($output);
  193.         $blah = explode(' ', $output);
  194.         $excerpt_length = 120;
  195.         if (count($blah) > $excerpt_length) {
  196.             $k = $excerpt_length;
  197.             $use_dotdotdot = 1;
  198.         } else {
  199.             $k = count($blah);
  200.             $use_dotdotdot = 0;
  201.         }
  202.         $excerpt = '';
  203.         for ($i=0; $i<$k; $i++) {
  204.             $excerpt .= $blah[$i].' ';
  205.         }
  206.         $excerpt .= ($use_dotdotdot) ? '...' : '';
  207.         $output = $excerpt;
  208.     } // end if no excerpt
  209.     return $output;
  210. }
  211.  
  212. function wp_link_pages($args = '') {
  213.     parse_str($args, $r);
  214.     if (!isset($r['before'])) $r['before'] = '<p>' . __('Pages:');
  215.     if (!isset($r['after'])) $r['after'] = '</p>';
  216.     if (!isset($r['next_or_number'])) $r['next_or_number'] = 'number';
  217.     if (!isset($r['nextpagelink'])) $r['nextpagelink'] = 'Next page';
  218.     if (!isset($r['previouspagelink'])) $r['previouspagelink'] = 'Previous page';
  219.     if (!isset($r['pagelink'])) $r['pagelink'] = '%';
  220.     if (!isset($r['more_file'])) $r['more_file'] = '';
  221.     link_pages($r['before'], $r['after'], $r['next_or_number'], $r['nextpagelink'], $r['previouspagelink'], $r['pagelink'], $r['more_file']);
  222. }
  223.  
  224. function link_pages($before='<br />', $after='<br />', $next_or_number='number', $nextpagelink='next page', $previouspagelink='previous page', $pagelink='%', $more_file='') {
  225.     global $id, $page, $numpages, $multipage, $more;
  226.     global $pagenow;
  227.     global $querystring_start, $querystring_equal, $querystring_separator;
  228.     if ($more_file != '') {
  229.         $file = $more_file;
  230.     } else {
  231.         $file = $pagenow;
  232.     }
  233.     if (($multipage)) {
  234.         if ($next_or_number=='number') {
  235.             echo $before;
  236.             for ($i = 1; $i < ($numpages+1); $i = $i + 1) {
  237.                 $j=str_replace('%',"$i",$pagelink);
  238.                 echo ' ';
  239.                 if (($i != $page) || ((!$more) && ($page==1))) {
  240.                 if ('' == get_settings('permalink_structure')) {
  241.                     echo '<a href="'.get_permalink().$querystring_separator.'page'.$querystring_equal.$i.'">';
  242.                 } else {
  243.                     echo '<a href="'.get_permalink().$i.'/">';
  244.                 }
  245.                 }
  246.                 echo $j;
  247.                 if (($i != $page) || ((!$more) && ($page==1)))
  248.                     echo '</a>';
  249.             }
  250.             echo $after;
  251.         } else {
  252.             if ($more) {
  253.                 echo $before;
  254.                 $i=$page-1;
  255.                 if ($i && $more) {
  256.                 if ('' == get_settings('permalink_structure')) {
  257.                     echo '<a href="'.get_permalink().$querystring_separator.'page'.$querystring_equal.$i.'">';
  258.                 } else {
  259.                     echo '<a href="'.get_permalink().$i.'/">';
  260.                 }
  261.                 }
  262.                 $i=$page+1;
  263.                 if ($i<=$numpages && $more) {
  264.                 if ('' == get_settings('permalink_structure')) {
  265.                     echo '<a href="'.get_permalink().$querystring_separator.'page'.$querystring_equal.$i.'">';
  266.                 } else {
  267.                     echo '<a href="'.get_permalink().$i.'/">';
  268.                 }
  269.                 }
  270.                 echo $after;
  271.             }
  272.         }
  273.     }
  274. }
  275.  
  276.  
  277. function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
  278.     global $tableposts, $id, $post, $wpdb;
  279.     global $p, $posts, $posts_per_page, $s, $single;
  280.     global $querystring_start, $querystring_equal, $querystring_separator;
  281.  
  282.     if(($p) || ($posts_per_page == 1) || 1 == $single) {
  283.  
  284.         $current_post_date = $post->post_date;
  285.         $current_category = $post->post_category;
  286.  
  287.         $sqlcat = '';
  288.         if ($in_same_cat != 'no') {
  289.             $sqlcat = " AND post_category = '$current_category' ";
  290.         }
  291.  
  292.         $sql_exclude_cats = '';
  293.         if (!empty($excluded_categories)) {
  294.             $blah = explode('and', $excluded_categories);
  295.             foreach($blah as $category) {
  296.                 $category = intval($category);
  297.                 $sql_exclude_cats .= " AND post_category != $category";
  298.             }
  299.         }
  300.  
  301.         $limitprev--;
  302.         $lastpost = @$wpdb->get_row("SELECT ID, post_title FROM $tableposts WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT $limitprev, 1");
  303.         if ($lastpost) {
  304.             $string = '<a href="'.get_permalink($lastpost->ID).'">'.$previous;
  305.             if ($title == 'yes') {
  306.                 $string .= wptexturize(stripslashes($lastpost->post_title));
  307.             }
  308.             $string .= '</a>';
  309.             $format = str_replace('%', $string, $format);
  310.             echo $format;
  311.         }
  312.     }
  313. }
  314.  
  315. function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
  316.     global $tableposts, $posts_per_page, $post, $wpdb, $single;
  317.     if(1 == $posts_per_page || 1 == $single) {
  318.  
  319.         $current_post_date = $post->post_date;
  320.         $current_category = $post->post_category;
  321.  
  322.         $sqlcat = '';
  323.         if ($in_same_cat != 'no') {
  324.             $sqlcat = " AND post_category='$current_category' ";
  325.         }
  326.  
  327.         $sql_exclude_cats = '';
  328.         if (!empty($excluded_categories)) {
  329.             $blah = explode('and', $excluded_categories);
  330.             foreach($blah as $category) {
  331.                 $category = intval($category);
  332.                 $sql_exclude_cats .= " AND post_category != $category";
  333.             }
  334.         }
  335.  
  336.         $now = current_time('mysql');
  337.  
  338.         $limitnext--;
  339.  
  340.         $nextpost = @$wpdb->get_row("SELECT ID,post_title FROM $tableposts WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT $limitnext,1");
  341.         if ($nextpost) {
  342.             $string = '<a href="'.get_permalink($nextpost->ID).'">'.$next;
  343.             if ($title=='yes') {
  344.                 $string .= wptexturize(stripslashes($nextpost->post_title));
  345.             }
  346.             $string .= '</a>';
  347.             $format = str_replace('%', $string, $format);
  348.             echo $format;
  349.         }
  350.     }
  351. }
  352.  
  353. function next_posts($max_page = 0) { // original by cfactor at cooltux.org
  354.     global $p, $paged, $what_to_show, $pagenow;
  355.     global $querystring_start, $querystring_equal, $querystring_separator;
  356.     if (empty($p) && ($what_to_show == 'paged')) {
  357.         $qstr = $_SERVER['QUERY_STRING'];
  358.         if (!empty($qstr)) {
  359.             $qstr = preg_replace('/&paged=\d{0,}/', '', $qstr);
  360.             $qstr = preg_replace('/paged=\d{0,}/', '', $qstr);
  361.         } elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) {
  362.             if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '',
  363.                                             $_SERVER['REQUEST_URI']) ) {
  364.                 $qstr = preg_replace('/^\//', '', $qstr);
  365.                 $qstr = preg_replace('/paged\/\d{0,}\//', '', $qstr);
  366.                 $qstr = preg_replace('/paged\/\d{0,}/', '', $qstr);
  367.                 $qstr = preg_replace('/\/$/', '', $qstr);
  368.             }
  369.         }
  370.         if (!$paged) $paged = 1;
  371.         $nextpage = intval($paged) + 1;
  372.         if (!$max_page || $max_page >= $nextpage) {
  373.             echo  get_settings('home') .'/'.$pagenow.$querystring_start.
  374.                 ($qstr == '' ? '' : $qstr.$querystring_separator) .
  375.                 'paged'.$querystring_equal.$nextpage;
  376.         }
  377.     }
  378. }
  379.  
  380. function next_posts_link($label='Next Page »', $max_page=0) {
  381.     global $p, $paged, $result, $request, $posts_per_page, $what_to_show, $wpdb;
  382.     if ($what_to_show == 'paged') {
  383.         if (!$max_page) {
  384.             $nxt_request = $request;
  385.             //if the query includes a limit clause, call it again without that
  386.             //limit clause!
  387.             if ($pos = strpos(strtoupper($request), 'LIMIT')) {
  388.                 $nxt_request = substr($request, 0, $pos);
  389.             }
  390.             $nxt_result = $wpdb->query($nxt_request);
  391.             $numposts = $wpdb->num_rows;
  392.             $max_page = ceil($numposts / $posts_per_page);
  393.         }
  394.         if (!$paged)
  395.             $paged = 1;
  396.         $nextpage = intval($paged) + 1;
  397.         if (empty($p) && (empty($paged) || $nextpage <= $max_page)) {
  398.             echo '<a href="';
  399.             next_posts($max_page);
  400.             echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
  401.         }
  402.     }
  403. }
  404.  
  405.  
  406. function previous_posts() { // original by cfactor at cooltux.org
  407.     global $_SERVER, $p, $paged, $what_to_show, $pagenow;
  408.     global $querystring_start, $querystring_equal, $querystring_separator;
  409.     if (empty($p) && ($what_to_show == 'paged')) {
  410.         $qstr = $_SERVER['QUERY_STRING'];
  411.         if (!empty($qstr)) {
  412.             $qstr = preg_replace('/&paged=\d{0,}/', '', $qstr);
  413.             $qstr = preg_replace('/paged=\d{0,}/', '', $qstr);
  414.         } elseif (stristr($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'] )) {
  415.             if ('' != $qstr = str_replace($_SERVER['SCRIPT_NAME'], '',
  416.                                             $_SERVER['REQUEST_URI']) ) {
  417.                 $qstr = preg_replace('/^\//', '', $qstr);
  418.                 $qstr = preg_replace("/paged\/\d{0,}\//", '', $qstr);
  419.                 $qstr = preg_replace('/paged\/\d{0,}/', '', $qstr);
  420.                 $qstr = preg_replace('/\/$/', '', $qstr);
  421.             }
  422.         }
  423.         $nextpage = intval($paged) - 1;
  424.         if ($nextpage < 1) $nextpage = 1;
  425.         echo  get_settings('home') .'/'.$pagenow.$querystring_start.
  426.             ($qstr == '' ? '' : $qstr.$querystring_separator) .
  427.             'paged'.$querystring_equal.$nextpage;
  428.     }
  429. }
  430.  
  431. function previous_posts_link($label='« Previous Page') {
  432.     global $p, $paged, $what_to_show;
  433.     if (empty($p)  && ($paged > 1) && ($what_to_show == 'paged')) {
  434.         echo '<a href="';
  435.         previous_posts();
  436.         echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
  437.     }
  438. }
  439.  
  440. function posts_nav_link($sep=' :: ', $prelabel='<< Previous Page', $nxtlabel='Next Page >>') {
  441.     global $p, $what_to_show, $request, $posts_per_page, $wpdb;
  442.     if (empty($p) && ($what_to_show == 'paged')) {
  443.         $nxt_request = $request;
  444.         if ($pos = strpos(strtoupper($request), 'LIMIT')) {
  445.             $nxt_request = substr($request, 0, $pos);
  446.         }
  447.         $nxt_result = $wpdb->query($nxt_request);
  448.         $numposts = $wpdb->num_rows;
  449.         $max_page = ceil($numposts / $posts_per_page);
  450.         if ($max_page > 1) {
  451.             previous_posts_link($prelabel);
  452.             echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
  453.             next_posts_link($nxtlabel, $max_page);
  454.         }
  455.     }
  456. }
  457.  
  458. /*
  459.  * Post-meta: Custom per-post fields.
  460.  */
  461.  
  462. function get_post_custom() {
  463.     global $id, $post_meta_cache;
  464.  
  465.     return $post_meta_cache[$id];
  466. }
  467.  
  468. function get_post_custom_keys() {
  469.     global $id, $post_meta_cache;
  470.     
  471.     if (!is_array($post_meta_cache[$id]))
  472.         return;
  473.     if ($keys = array_keys($post_meta_cache[$id]))
  474.         return $keys;
  475. }
  476.  
  477. function get_post_custom_values($key='') {
  478.     global $id, $post_meta_cache;
  479.  
  480.     return $post_meta_cache[$id][$key];
  481. }
  482.  
  483. // this will probably change at some point...
  484. function the_meta() {
  485.     global $id, $post_meta_cache;
  486.     
  487.     if ($keys = get_post_custom_keys()) {
  488.         echo "<ul class='post-meta'>\n";
  489.         foreach ($keys as $key) {
  490.             $values = array_map('trim',$post_meta_cache[$id][$key]);
  491.             $value = implode($values,', ');
  492.             
  493.             echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
  494.         }
  495.         echo "</ul>\n";
  496.     }
  497. }
  498.  
  499. ?>